package com.kcht.sylyg08.service;

import com.kcht.adp.common.log.Logger;
import com.kcht.adp.pagination.Page;
import com.kcht.sylyg08.ctrl.vo.ContainerVO;
import com.kcht.sylyg08.ctrl.vo.InventoryInfo;
import com.kcht.sylyg08.dao.IContainerDao;
import com.kcht.sylyg08.dao.IContainerStatusTransformRecordDao;
import com.kcht.sylyg08.dao.ICurrentContainerStatusDao;
import com.kcht.sylyg08.utils.ExcelUtil;
import com.kcth.sylyg08.entity.Container;
import com.kcth.sylyg08.entity.ContainerStatus;
import com.kcth.sylyg08.entity.ContainerStatusTransformRecord;
import com.kcth.sylyg08.entity.CurrentContainerStatus;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@RequestScoped
@Transactional()
public class ContainerService {
    @Inject
    private Logger logger;
    @EJB
    private IContainerDao dao;
    @EJB
    private IContainerStatusTransformRecordDao cstrDao;
    @EJB
    private ICurrentContainerStatusDao ccsDao;

    public void save(Container container) {
        if (container.getId() == null) {
            dao.save(container);
            initContainerStatus(container.getContainerId());
        } else {
            dao.merge(container);
        }
    }

    public Page<ContainerVO, ContainerVO> list(Page<ContainerVO, ContainerVO> page) {
        if ("".equals(page.getParameter().getIssuePerson())) {
            page.getParameter().setIssuePerson(null);
        }
        return dao.getPageListBySQL("Container.queryContainerList", page, ContainerVO.class);
    }

    public void delete(List<Integer> ids) {
        ids.forEach(id -> {
            Container container = dao.get(id.longValue());
            if (Objects.nonNull(container)) {
                dao.delete(id.longValue());
                CurrentContainerStatus status = ccsDao.getByContainerId(container.getContainerId());
                ccsDao.delete(status.getId());
                cstrDao.deleteByContainerId(container.getContainerId());
            }
        });
    }

    public String importContainerByExcel(File file) {
        String msg = null;
        try {
            logger.info("开始处理excel文件。");
            List<String[]> rows = ExcelUtil.readExcel(file);
            Container container;
            int count = 0;
            for (String[] cells : rows) {
                if ("发行完成".equals(cells[4]) && Objects.isNull(container = dao.get(cells[1]))) {
                    container = new Container();
                    container.setContainerId(cells[1]);
                    switch (cells[1].substring(7, 8)) {
                        case "1":
                            container.setContainerType("副车架");
                            break;
                        case "2":
                            container.setContainerType("扭梁");
                            break;
                        case "3":
                            container.setContainerType("前轴");
                    }
                    dao.save(container);
                    initContainerStatus(container.getContainerId());
                    logger.info("添加料框" + cells[1] + "成功。");
                    count++;
                }
            }
            msg = "处理完成：本次共添加" + count + "个料框。";
            logger.info(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return msg;
    }

    public List<InventoryInfo> getInventoryInfo() {
        return dao.selectListBySQL("select d.containerType as containerType , d.material as material , sum(d.count) as count " +
                "from (select a.containerId containerId,a.containerType containerType,c.material material , c.count count , b.status status from t_lzwl_container a " +
                "left join t_lzwl_currentContainerStatus b on a.containerId = b.containerId " +
                "left join t_lzwl_materialcontainerbound c on a.containerId = c.containerId " +
                "where b.status = 3) d " +
                " group by d.material", InventoryInfo.class);
    }

    private void initContainerStatus(String containerId) {
        ContainerStatusTransformRecord record = new ContainerStatusTransformRecord();
        record.setTransformDate(new Date());
        record.setContainerId(containerId);
        record.setIsAbnormal(false);
        cstrDao.save(record);
        CurrentContainerStatus status = new CurrentContainerStatus();
        status.setContainerId(containerId);
        status.setToBeMoven(false);
        //status.setStatus(ContainerStatus.初始空闲);
        ccsDao.save(status);
    }
}
